﻿[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.CustomConstraint.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.Additions.CustomConstraint.debug.js", "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.Additions
{    
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Helper;
    using BM.Tools.WebControls.MForm.Controls.Items;
    
    /// <summary>
    /// A custom constraint that can be created on the item
    /// Very similiar to the <see cref="CustomValidator"/> but works with the MForm item controls validation.
    /// </summary>
    [ClientScriptResource("BM.Tools.WebControls.MForm",
            "BM.Tools.WebControls.MForm.Controls.Additions.CustomConstraint.js")]
    public class CustomConstraint : Constraint
    {
        /// <summary>
        /// The server validate event handler
        /// </summary>
        [SuppressMessage("Microsoft.Design", "CA1009:DeclareEventHandlersCorrectly", Justification = "Cannot change the event definition - it is a Microsoft code")]
        public event ServerValidateEventHandler ServerValidate;

        /// <summary>
        /// Gets or sets the client function name.
        /// </summary>
        [AttributeProperty(Name = "name")]
        [DefaultValue("")]
        public string ClientFunctionName
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the default error text.
        /// </summary>
        protected override string DefaultErrorText
        {
            get
            {
                return Resources.MessageResource.CustomConstraintError;
            }
        }

        /// <summary>
        /// Checks the validity of the item
        /// </summary>
        /// <param name="item">
        /// The checked item.
        /// </param>
        /// <returns>
        /// is the constraint met for the item
        /// </returns>
        public override bool IsValid(OffspringItem item)
        {            
            var leafItem = item as LeafItem;
            var value = (leafItem != null) ? leafItem.Value : null;            
            return OnServerValidate(value);
        }

        /// <summary>
        /// performs server validation using the validation handler
        /// </summary>
        /// <param name="value">
        /// The value.
        /// </param>
        /// <returns>
        /// is the server validation met
        /// </returns>
        protected virtual bool OnServerValidate(string value)
        {                        
            if (ServerValidate != null)
            {
                var args = new ServerValidateEventArgs(value, true);
                ServerValidate(this, args);
                return args.IsValid;
            }

            return true;
        }
    }
}